package io.renren.common.utils;

import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;

/**
 * ZIP文件解压
 * @author Administrator
 *
 */
public class ZipUncompress {
	  
	 private static int bufSize = 8096;    //size of byte
	 
	 public static void main(String[] args) throws Exception  
	    {  
	        
	  
	        String tmpFile = "E:\\down\\param\\Rate\\20171114\\44120088_DownLoad_Rate_20171108102837_RES_11_0.rar";  
	        ZipUncompress.unZip(tmpFile, tmpFile.substring(0, tmpFile.lastIndexOf(".")));  
	        // -----------------  
	        System.out.println("Usage:");  
	        System.out.println("解压:java ZipFileUtils -unzip fileName.zip");  
	        System.out.println();  
	    }  
	 public static String unZip(String unZipFileName, String destFileName)  
	    {  
	        File unzipFile = new File(unZipFileName);  
	  
	        if(destFileName == null || destFileName.trim().length() == 0)  
	        {  
	            destFileName = unzipFile.getParent();  
	        }  
	  
	        File destFile;  
	        ZipFile zipFile = null;  
	        try  
	        {  
	            zipFile = new ZipFile(unzipFile, "UTF-8");  
	            for(Enumeration entries = zipFile.getEntries(); entries.hasMoreElements();)  
	            {  
	                ZipEntry entry = (ZipEntry) entries.nextElement();  
	                destFile = new File(destFileName, entry.getName());  
	  
	                unZipFile(destFile, zipFile, entry); //执行解压  
	            }  
	        } catch(Exception e)  
	        {  
	            return e.getMessage();  
	        }  
	        finally  
	        {  
	            try  
	            {  
	                assert zipFile != null;  
	                zipFile.close();  
	            } catch(Exception e)  
	            {  
	            	e.printStackTrace();
	            }  
	        }  
	        return null;  
	    }  
	 private static void unZipFile(File destFile, ZipFile zipFile, ZipEntry entry)  
		        throws IOException  
		    {  
		        InputStream inputStream;  
		        FileOutputStream fileOut;  
		        if(entry.isDirectory()) //是目录，则创建之  
		        {  
		            destFile.mkdirs();  
		        }  
		        else //是文件  
		        {  
		            //如果指定文件的父目录不存在,则创建之.  
		            File parent = destFile.getParentFile();  
		            if(parent != null && !parent.exists())  
		            {  
		                parent.mkdirs();  
		            }  
		  
		            inputStream = zipFile.getInputStream(entry);  
		  
		            fileOut = new FileOutputStream(destFile);  
		            byte[] buf = new byte[bufSize];  
		            int readedBytes;  
		            while((readedBytes = inputStream.read(buf)) > 0)  
		            {  
		                fileOut.write(buf, 0, readedBytes);  
		            }  
		            fileOut.close();  
		  
		            inputStream.close();  
		        }  
		    } 
}     



